package qmm.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Cookie;

import qmm.dao.UserDao;
import qmm.entity.Goods;
import qmm.entity.User;
import qmm.util.JdbcUtil;

public class UserDaoImpl implements UserDao {

	@Override
	public int checkAccount(String account) {
		int n = 0;
		//建立连接
		Connection conn = JdbcUtil.getConnection();
		//写sql
		String sql = "select count(*) from t_user where account='"+account+"'";
		//创建st对象
		Statement st;
		try {
			st = conn.createStatement();
			//执行sql
			ResultSet rs = st.executeQuery(sql);
			//处理结果
			if(rs.next()){
				//解析一行
				n = rs.getInt(1);//取第一列的值
			
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return n;
	}

	@Override
	public int checkCard(String card) {
		int n = 0;
		try {
			Connection conn = JdbcUtil.getConnection();
			//写sql
			String sql = "select count(*) from t_user where id_card_no='"+card+"'";
			//创建st对象
			Statement st = conn.createStatement();
			//执行sql
			ResultSet rs = st.executeQuery(sql);
			//处理结果
			if(rs.next()){
				//解析一行
				n = rs.getInt(1);//取第一列的值
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return n;
	}

	@Override
	public int checkEmail(String email) {
		int n = 0;
		try {
			Connection conn = JdbcUtil.getConnection();
			//写sql
			String sql = "select count(*) from t_user where email='"+email+"'";
			//创建st对象
			Statement st = conn.createStatement();
			//执行sql
			ResultSet rs = st.executeQuery(sql);
			//处理结果
			if(rs.next()){
				//解析一行
				n = rs.getInt(1);//取第一列的值	
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return n;
	}

	@Override
	public int checkPhone(String phone) {
		int n = 0;
		try {
			Connection conn = JdbcUtil.getConnection();
			//写sql
			String sql = "select count(*) from t_user where phone='"+phone+"'";
			//创建st对象
			Statement st = conn.createStatement();
			//执行sql
			ResultSet rs = st.executeQuery(sql);
			//处理结果
			if(rs.next()){
				//解析一行
				n = rs.getInt(1);//取第一列的值			
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return n;
	}

	@Override
	public int checkPwd(String password, int id) {
		int n = 0;
		String sql = "select count(*) from t_user where password='"+password+"' and id = "+id;
		Connection conn = JdbcUtil.getConnection();
		Statement st;
		try {
			st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			if(rs.next()){
				n = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return n;
	}

	@Override
	public int editCard(String card, int id) {
		String sql = "update t_user set id_card_no='"+card+"' where id="+id;
		System.out.println(sql);
		return JdbcUtil.update(sql);
	}

	@Override
	public int editEmail(String email, int id) {
		String sql = "update t_user set email='"+email+"' where id="+id;
		System.out.println(sql);
		return JdbcUtil.update(sql);
	}

	@Override
	public int editNickName(String nickname, int id) {
		String sql = "update t_user set nick_name='"+nickname+"' where id="+id;
		return  JdbcUtil.update(sql);
	}

	@Override
	public int editPassword(String password, int id) {
		String sql = "update t_user set password = '"+password+"' where id = "+id;
		return JdbcUtil.update(sql);
	}

	@Override
	public int editPhone(String phone, int id) {
		String sql = "update t_user set phone='"+phone+"' where id="+id;
		System.out.println(sql);
		return JdbcUtil.update(sql);
	}

	@Override
	public int editFace(String face, int id) {
		String sql = "update t_user set face = '"+face+"' where id = "+id;
		return JdbcUtil.update(sql);
	}

	@Override
	public User login(String account, String password) {
		User user = null;
		try {
			Connection conn = JdbcUtil.getConnection();
			//写sql
			String sql = "select * from t_user where is_lock=0 and account='"+account+"' and password='"+password+"'";
			//创建st对象
			Statement st = conn.createStatement();
			//执行sql
			ResultSet rs = st.executeQuery(sql);
			//处理结果
			if(rs.next()){
				//解析一行
				int id = rs.getInt("id");
				String account2 = rs.getString("account");
				String nickname = rs.getString("nick_name");
				String password2 = rs.getString("password");
				int gender = rs.getInt("gender");
				String idCardNo = rs.getString("id_card_no");
				String email = rs.getString("email");
				String phone = rs.getString("phone");
				int type = rs.getInt("type");
				String face = rs.getString("face");
				//封装一行数据到实体对象中
				user = new User(id, account2, nickname, password2, gender, idCardNo, email, phone, type,face);
			}
		}catch (SQLException e) {
			e.printStackTrace();
		}
		return user;
	}

	@Override
	public int add(String account, String password, String email, String phone) {
		String sql = "INSERT INTO t_user ( account,password,nick_name,gender ,email, phone) VALUES ( '"+account+"',  '"+password+"', '佚名',1,  '"+email+"', '"+phone+"')";
		System.out.println(sql);
		return JdbcUtil.update(sql);
	}

	@Override
	public List<User> findAll(int page,int size,String account,String phone) {
		List<User> list = new ArrayList<User>();
		int n = (page-1)*size;
		String sql = "select * from t_user where 1=1 ";
		if(account!=null&&account!=""){
			sql+=" and account='"+account+"'";
		}
		if(phone!=null&&phone!=""){
			sql+=" and phone='"+phone+"'";
		}
		sql+=" limit "+n+","+size;
		Connection conn = JdbcUtil.getConnection();
		Statement st;
		try {
			st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			while(rs.next()){
				int id = rs.getInt("id");
				String account2 = rs.getString("account");
				String nickname = rs.getString("nick_name");
				String password = rs.getString("password");
				int gender = rs.getInt("gender");
				String idCardNo = rs.getString("id_card_no");
				String email = rs.getString("email");
				String phone2 = rs.getString("phone");
				String face = rs.getString("face");
				int type = rs.getInt("type");
				int isLock = rs.getInt("is_lock");
				User user = new User(id, account2, nickname, password, gender, idCardNo, email, phone2, type, isLock, face);
				list.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public int getCount(String account, String phone) {
		int n = 0;
		String sql = "select count(*) from t_user where 1=1 ";
		if(account!=null&&account!=""){
			sql+=" and account='"+account+"'";
		}
		if(phone!=null&&phone!=""){
			sql+=" and phone='"+phone+"'";
		}
		Connection conn = JdbcUtil.getConnection();
		Statement st;
		try {
			st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			if(rs.next()){
				n = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return n;
	}

	@Override
	public int lock(int id) {
		String sql="update t_user set is_lock=abs(is_lock-1) where id="+id;    																							
		System.out.println(sql);
		return JdbcUtil.update(sql);
	}

	@Override
	public int resetPwd(int id) {
		String sql="update t_user set password='123456' where id="+id;    																							
		System.out.println(sql);
		return JdbcUtil.update(sql);
	}

}
